package com.ckjw.gobang.view;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;

import com.ckjw.gobang.data.Player;


/**
 * 显示游戏用户及相关信息
 * @author lck and djw
 * @Date 2022年6月4日-下午10:02:14
 */
public class UserPanel extends JPanel{
    private static final long serialVersionUID = 1L;
    
    //定义用户面板所用到的所有个组件
    static JPanel myPanel, otherPanel;
    private static JLabel myName, myAddress, myColor, myGrade;
    private static JLabel otherName, otherAddress, otherColor, otherGrade;
    public static UserPanel userPanel;//供外部调用
    /**
     * 用户信息面板，左面版(我的信息)
     */
    public static final int left = 0;
    /**
     * 用户信息面板，右面版(对方信息)
     */
    public static final int right = 1;
    
    /**
     * 定义构造期器
     */
    public UserPanel()
    {
    	//this.setVisible(false);
    	basicSettings();
    }
    /**
     * 用户面板的基本设置
     */
    private  void basicSettings() {
    	this.setLayout(null);
        this.setBackground(new Color(180, 180, 180));
    	//创建两个用户面板
        myPanel = new JPanel();
        otherPanel = new JPanel();
        myPanel.setLayout(new GridLayout(4, 1));
        otherPanel.setLayout(new GridLayout(4, 1));
        //当前用户面板所放组件
        myName = new JLabel("玩家昵称: ");
        myAddress = new JLabel("玩家地址: ");
        myColor = new JLabel("玩家棋色: ");
        myGrade = new JLabel("玩家分数: ");
        //其它玩家所放组件
        otherName = new JLabel("玩家昵称: ");
        otherAddress = new JLabel("玩家地址: ");
        otherColor = new JLabel("玩家棋色: ");
        otherGrade = new JLabel("玩家分数: ");
        
        myPanel.add(myName);
        myPanel.add(myAddress);
        myPanel.add(myColor);
        myPanel.add(myGrade);

        otherPanel.add(otherName);
        otherPanel.add(otherAddress);
        otherPanel.add(otherColor);
        otherPanel.add(otherGrade);
        
        this.add(myPanel);
        this.add(otherPanel);
        userPanel = this;
	}
    /**
     * 界面显示，控件加载完毕后执行
     */
    public static void init() {
        myPanel.setBounds(2, 2, userPanel.getWidth() / 2 - 2, userPanel.getHeight() - 5);
        otherPanel.setBounds(userPanel.getWidth() / 2 + 2, 2, userPanel.getWidth() / 2 - 2,
                userPanel.getHeight() - 5);
        Player.init();
        setUserInfo(Player.myPlayer, left);
        setUserInfo(Player.otherPlayer, right);
        userPanel.repaint();
    }
    
  /**
   * 设置用户信息
   * @param player 玩家对象
   * @param position 在面板的位置
   */
	public static void setUserInfo(Player player, int position) {
		// TODO Auto-generated method stub
		if (player == null) {
            player = new Player();
        }
        if (position == left) {
            myName.setText("玩家昵称: " + player.getName());
            myAddress.setText("玩家地址: " + player.getAddress());
            myColor.setText("玩家棋色: " + player.getColorString());
            myGrade.setText("玩家分数: " + player.getGrade());
        } else {
            otherName.setText("玩家昵称: " + player.getName());
            otherAddress.setText("玩家地址: " + player.getAddress());
            otherColor.setText("玩家棋色: " + player.getColorString());
            otherGrade.setText("玩家分数: " + player.getGrade());
        }
	}
   /**
    * 设置信息
    * @param grade 分数
    * @param position 面板位置
    */
    public static void setGrade(int grade, int position) {
        if (position == left) {
            myGrade.setText("玩家分数: " + grade);
        } else {
            otherGrade.setText("玩家分数: " + grade);
        }
    }

}
